package com.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.dto.ProposalDto;
import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;
import com.insurance.dao.ProposalDao;
import com.insurance.entity.ProposalEntity;
import com.insurance.service.ProposalService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("proposalService")
public class ProposalServiceImpl extends ServiceImpl<ProposalDao, ProposalEntity> implements ProposalService {

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProposalEntity> page = this.page(
                new Query<ProposalEntity>().getPage(params),
                new QueryWrapper<ProposalEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取查询条件
     *
     * @return
     */
    public QueryWrapper<ProposalEntity> getQueryWrapper(){
        QueryWrapper<ProposalEntity> queryWrapper = new QueryWrapper<ProposalEntity>();
        queryWrapper.orderByDesc("create_time");
        return queryWrapper;
    }

    /**
     * 通过实体获取查询条件
     *
     * @param proposalEntity 实体
     * @return
     */
    public QueryWrapper<ProposalEntity> getQueryWrapper(ProposalEntity proposalEntity){
        QueryWrapper<ProposalEntity> queryWrapper = getQueryWrapper();
        if(!StringUtils.isBlank(proposalEntity.getProposalId())){
            queryWrapper.eq("proposal_id", proposalEntity.getProposalId());
        }
        if(!StringUtils.isBlank(proposalEntity.getCreatePersonId())){
            queryWrapper.eq("create_person_id", proposalEntity.getCreatePersonId());
        }
        if(!StringUtils.isBlank(proposalEntity.getAppId())){
            queryWrapper.eq("app_id", proposalEntity.getAppId());
        }
        if(!StringUtils.isBlank(proposalEntity.getState())){
            queryWrapper.eq("state", proposalEntity.getState());
        }
        if(!StringUtils.isBlank(proposalEntity.getProductId())){
            queryWrapper.eq("product_id", proposalEntity.getProductId());
        }
        if(!StringUtils.isBlank(proposalEntity.getProposalNo())){
            queryWrapper.eq("proposal_no", proposalEntity.getProposalNo());
        }
        if(!StringUtils.isBlank(proposalEntity.getInvoiceStatus())){
            queryWrapper.eq("invoice_status", proposalEntity.getInvoiceStatus());
        }
        queryWrapper.eq("delete_flag", String.valueOf(DeleteFlagEnum.UN_DELETE.getCode()));
        return queryWrapper;
    }

    /**
     * 通过查询条件查询所有
     *
     * @param queryWrapper
     * @return
     */
    public List<ProposalEntity> findAll(QueryWrapper<ProposalEntity> queryWrapper){
        return this.list(queryWrapper);
    }

    /**
     * 通过用户id查询投保单
     *
     * @param page 分页条件
     * @param userId 用户id
     * @param productName 产品名称
     * @return
     */
    @Override
    public Page<ProposalEntity> findByUserId(Page page, String userId, String productName){
        ProposalEntity proposalEntity = new ProposalEntity();
        proposalEntity.setCreatePersonId(userId);
        proposalEntity.setProductName(productName);
        return this.queryProposalPage(page, proposalEntity);
    }

    /**
     * 通过用户id查询投保单
     *
     * @param page 分页条件
     * @param userId 用户id
     * @param productName 产品名称
     * @param state 投保单状态
     * @return
     */
    @Override
    public Page<ProposalEntity> findByUserId(Page page, String userId, String productName, String state){
        ProposalEntity proposalEntity = new ProposalEntity();
        proposalEntity.setCreatePersonId(userId);
        proposalEntity.setProductName(productName);
        proposalEntity.setState(state);
        return this.queryProposalPage(page, proposalEntity);
    }

    /**
     * 分页查询投保单列表
     *
     * @param page 分页条件
     * @param proposalEntity 查询条件
     * @return
     */
    public Page<ProposalEntity> queryProposalPage(Page page, ProposalEntity proposalEntity){
        QueryWrapper<ProposalEntity> queryWrapper = getQueryWrapper(proposalEntity);
        return this.page(page, queryWrapper);
    }

    /**
     * 分页查询
     * @param page 分页条件
     * @param proposalEntity 查询条件
     * @return
     */
    @Override
    public Page<ProposalDto> queryProposalDtoPage(Page page, ProposalEntity proposalEntity){
        QueryWrapper<ProposalEntity> queryWrapper = getQueryWrapper(proposalEntity);
        return this.page(page, queryWrapper);
    }

    /**
     * 通过用户主键查询订单列表
     *
     * @param page 分页条件
     * @param userId 用户主键
     * @param productId 产品主键
     * @param appId 来源
     * @return
     */
    @Override
    public Page<ProposalEntity> queryProposalPageByUserId(Page page, String userId, String productId, String appId){
        ProposalEntity proposalEntity = new ProposalEntity();
        proposalEntity.setCreatePersonId(userId);
        proposalEntity.setProductId(productId);
        proposalEntity.setAppId(appId);
        return this.queryProposalPage(page, proposalEntity);
    }

    /**
     * 接口通过用户主键查询订单列表
     *
     * @param page 分页条件
     * @param userId 用户主键
     * @param productId 产品主键
     * @param appId 来源
     * @return
     */
    @Override
    public Page<ProposalDto> queryIProposalPage(Page page, String userId, String productId, String appId){
        ProposalEntity proposalEntity = new ProposalEntity();
        proposalEntity.setCreatePersonId(userId);
        proposalEntity.setProductId(productId);
        proposalEntity.setAppId(appId);
        QueryWrapper<ProposalEntity> queryWrapper = getQueryWrapper(proposalEntity);
        return this.page(page, queryWrapper);
    }

}